home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ian & Stuart's Australian Mac 1993 September
/
September 93.iso
/
Archives
/
Sound
/
MIDI
/
MIDI Utilities
/
CMU Midi Toolkit
/
Source
/
filestream.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-01-11
|
7KB
|
251 lines
#include "switches.h"
#ifdef LIGHTSPEED
#include "Proto.h"
#endif
#include <StdIO.h>
#include "cext.h"
#include "userio.h"
#include "stream.h"
#include "filestream.h"
#define READ_ID 30897
#define WRITE_ID 16354
typedef struct my_data {
StreamProcs procs; /* pointers to generic functions */
ushort ID; /* indicates stream type */
FILE * fp; /* file pointer */
} *SelfPtr;
void filestream_close(SelfPtr);
int filestream_getchar(SelfPtr);
boolean filestream_getline(SelfPtr, char *, ushort);
ulong filestream_getpos(SelfPtr);
void filestream_notImplemented(void);
void filestream_putchar(SelfPtr, int);
void filestream_putstring(SelfPtr, char *);
void filestream_setpos(SelfPtr, ulong);
/****************************************************************************
* filestream_OpenRead
* Inputs:
* FILE * readFile: file to read from
* Returns:
* StreamPtr: newly created file read stream
* Effect:
* creates a new read stream on readFile.
* returns NULL if readFile is NULL or if there is no more memory.
****************************************************************************/
public StreamPtr filestream_OpenRead(readFile)
FILE *readFile;
{
SelfPtr newStream;
if (readFile == NULL) return NULL;
newStream = (SelfPtr) malloc(sizeof(struct my_data));
if (newStream == NULL) return NULL;
newStream->procs.close = filestream_close;
newStream->procs.getchr = filestream_getchar;
newStream->procs.getline = filestream_getline;
newStream->procs.getpos = filestream_getpos;
newStream->procs.putchr = filestream_notImplemented;
newStream->procs.putstring = filestream_notImplemented;
newStream->procs.setpos = filestream_setpos;
newStream->ID = READ_ID;
newStream->fp = readFile;
return (StreamPtr) newStream;
}
/****************************************************************************
* filestream_OpenWrite
* Inputs:
* FILE * writeFile: file to write to
* Returns:
* StreamPtr: newly created file write stream
* Effect:
* creates a new write stream on writeFile.
* returns NULL if writeFile is NULL or if there is no more memory.
****************************************************************************/
public StreamPtr filestream_OpenWrite(writeFile)
FILE *writeFile;
{
SelfPtr newStream;
if (writeFile == NULL) return NULL;
newStream = (SelfPtr) malloc(sizeof(struct my_data));
if (newStream == NULL) return NULL;
newStream->procs.close = filestream_close;
newStream->procs.getchr = (int (*)()) filestream_notImplemented;
newStream->procs.getline = (boolean (*)()) filestream_notImplemented;
newStream->procs.getpos = filestream_getpos;
newStream->procs.putchr = filestream_putchar;
newStream->procs.putstring = filestream_putstring;
newStream->procs.setpos = filestream_setpos;
newStream->ID = WRITE_ID;
newStream->fp = writeFile;
return (StreamPtr) newStream;
}
/****************************************************************************
* filestream_close
* Inputs:
* SelfPtr self: a file stream (read or write)
* Effect:
* closes the file and frees the data structure.
* writes a terminating character to the file if this is a write stream.
****************************************************************************/
private void filestream_close(self)
SelfPtr self;
{
if (self == NULL) return;
if (self->ID == WRITE_ID) {
fputc('\004', self->fp);
}
fclose(self->fp);
free((char *) self);
}
/****************************************************************************
* filestream_getchar
* Inputs:
* SelfPtr self: a file read stream
* Returns:
* int: value of character read or -1 (EOF) if at the end of file
* Effect:
* reads a character from the input file
****************************************************************************/
private int filestream_getchar(self)
SelfPtr self;
{
return fgetc(self->fp);
}
/****************************************************************************
* filestream_getline
* Inputs:
* SelfPtr self: a file read stream
* char * buff: string to read line into
* ushort buffSize: length of buff
* Returns:
* boolean: false if we are at the end of the file
* Effect:
* reads a line of characters from the input file. a line is terminated by
* either a newline or the end of the file. if buffSize is less
* than the length of a given line, only buffSize characters are consumed.
* (which could potentially cause the client program to think there are
* line breaks where there aren't; watch out!)
* NOTE: the terminating newline character is NOT part of the string
* returned by getline. (thus, the final line in the input file looks like
* any other line regardless of whether the input file ends in a newline.)
****************************************************************************/
private boolean filestream_getline(self, line, lineSize)
SelfPtr self;
char *line;
ushort lineSize;
{
register char *destPtr = line;
register char *maxDestPtr =
(destPtr + lineSize) - 1; /* leave room for terminator char */
register int ch;
if ((lineSize == 0) || (destPtr == NULL)) return false;
while (destPtr < maxDestPtr) {
ch = fgetc(self->fp);
if ((ch == '\n') || (ch == EOF)) {
break;
} else {
*destPtr++ = ch;
}
}
*destPtr = '\0';
return (ch != EOF);
}
/****************************************************************************
* filestream_getpos
* Inputs:
* SelfPtr self: a file stream (read or write)
* Returns:
* ulong: the number of characters since the beginning
****************************************************************************/
private ulong filestream_getpos(self)
SelfPtr self;
{
return ftell(self->fp);
}
/****************************************************************************
* filestream_notImplemented
* Effect:
* prints an error message and exits. used as a placeholder for functions
* that make no sense for a particular sort of stream (e.g. getchar for
* a write stream).
****************************************************************************/
private void filestream_notImplemented()
{
gprintf(FATAL, "(filestream.c): operation not implemented/n");
exit(-1);
}
/****************************************************************************
* filestream_putchar
* Inputs:
* SelfPtr self: a file write stream
* int ch: the integer value of the character to write
* Effect:
* writes ch to the file
****************************************************************************/
private void filestream_putchar(self, ch)
SelfPtr self;
int ch;
{
fputc((char) ch, self->fp);
}
/****************************************************************************
* filestream_putstring
* Inputs:
* SelfPtr self: a file write stream
* char * s: the string to put
* Effect:
* writes the given string to the file
****************************************************************************/
private void filestream_putstring(self, s)
SelfPtr self;
char *s;
{
fputs(s, self->fp);
}
/****************************************************************************
* filestream_setpos
* Inputs:
* SelfPtr self: a file stream (read or write)
* ulong newPos: the new position
* Effect:
* sets file position to newPos
****************************************************************************/
private void filestream_setpos(self, newPos)
SelfPtr self;
ulong newPos;
{
fseek(self->fp, newPos, 0);
}